import router from "@/router";
import axios from "axios";
import { API_BASE_URL } from "@/config";

console.log("API Base URL:", API_BASE_URL); // 检查 apiBaseUrl 的值

const api = axios.create({
    baseURL: API_BASE_URL, // API 基础路径
    timeout: 90000, // Set a timeout of 90 seconds
    // ensure cookies (and other credentials) are sent when backend requires them
    withCredentials: true,
});

// 请求拦截器
api.interceptors.request.use(
    (config) => {
        // 发送请求前的处理
        const token = window.sessionStorage.getItem('token');
        if (token) {
            // set standard Authorization header; some backends are case-sensitive
            config.headers['Authorization'] = token;
            // keep legacy header if any code expects it
            config.headers['AUTHORIZATION'] = token;
        }
        return config;
    },
    (error) => {
        // 请求错误的处理
        return Promise.reject(error);
    }
);
// 响应拦截器
api.interceptors.response.use((response) => {
    // 处理响应数据
    return response.data;
},
    (error) => {
        if (error.response) {
            switch (error.response.status) {
                case 400:
                    router.push("/error/400"); // 如果请求错误，重定向到400页面
                    console.error("Bad Request:", error.response);
                    break;
                case 401:
                    router.push("/error/401"); // 如果未授权，重定向到401页面
                    console.error("Unauthorized:", error.response);
                    break;
                case 403:
                    router.push("/error/403"); // 如果禁止访问，重定向到403页面
                    console.error("Forbidden:", error.response);
                    break;
                case 404:
                    router.push("/error/404"); // 如果未找到，重定向到404页面
                    console.error("Not Found:", error.response);
                    break;
                default:
                    console.error("Error:", error.response);
            }
        }
        // 处理响应错误
        return Promise.reject(error);
    }
);


/*网络请求部分*/

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        api({
            url: url,
            method: 'get',
            params: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        api({
            url: url,
            method: 'post',
            data: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
    });
}

/*
 *  delete请求
 *  url:请求地址
 *  params:参数
 * */
export function del(url, params = {}) {
    return new Promise((resolve, reject) => {
        api({
            url: url,
            method: 'delete',
            data: params
        }).then(response => {
            console.log(response)
            resolve(response);
        }).catch(error => {
            console.log(error)
            reject(error);
        });
    });
}


/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        api({
            url: url,
            method: 'post',
            data: params,
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}


export default {
    get,
    post,
    del,
    fileUpload,
    api
}
